//
//  MBCommodityDetailSpecCell.swift
//  iPhone
//
//  Created by Toj on 8/11/22.
//

import UIKit

private let discountLabelHeight: CGFloat = 14
// MARK: - 懒加载(属性), 变量, 初始化init
class MBCommodityDetailSpecCell: UICollectionViewCell {
    
    var model: MemberCommodityDto? { didSet {
        guard let commodity = model else { return }
        
        // 时间
        let date = "\(commodity.cs_valid_time_num)" + commodity.valid_time_en_name
        dateView.setTitle(date, for: .normal)
        
        let imgName = commodity.isSelected ? "commodity_border_sel" : ""
        backImageView.image = imgName.to_image
        dateView.isSelected = commodity.isSelected
        autoRenewalView.isHidden = !commodity.isAutoRenew
        giftImageView.isHidden = !commodity.promoting
        couponImageView.isHidden = !commodity.isCoupon
        
        // 商品吊牌价
        let style = NSMutableParagraphStyle()
        style.lineBreakMode = .byTruncatingTail
        style.alignment = .center
        let symbol = commodity.currency_symbol
        let price = commodity.price
        let font = UIFont.bold(with: 10.f_scale)
        let symbolAttributed = symbol.to_attributedString(font: font, color: priceLabel.textColor, paragraph: style)
        let priceAttributed = price.st_moneyFormat.st_cleanZero.to_attributedString(font: UIFont.boldSystemFont(ofSize: 21.f_scale), color: priceLabel.textColor, paragraph: style)
        symbolAttributed.append(priceAttributed)
        priceLabel.attributedText = symbolAttributed
        
        // 商品折扣价
        let list_price = commodity.list_price
        let list_priceString = symbol + list_price.st_moneyFormat.st_cleanZero
        let invalidPriceAttributed = list_priceString.to_attributedString(font: invalidPriceLabel.font, color: invalidPriceLabel.textColor, paragraph: style, lineStyle: .strikethrough)
        invalidPriceLabel.attributedText = invalidPriceAttributed
        invalidPriceLabel.isHidden = list_price <= price
        // 折扣
        var rate = ""
        if commodity.rate > 0.0 {
            rate = String(format: "%.0f%%", commodity.rate * 100)
            rate = String(format: OLLocalize("membership_activity_off"), rate)
        }
        discountLabel.text = rate
        discountLabel.isHidden = rate.isEmpty
        
        if(!commodity.isShowed) {
            commodity.isShowed = true
            let ext = [DSKExtension.channel_info.rawValue: commodity.channelInfo]
            let event = DSEvent(category: st_className, action: "ProdShow", label: commodity.name, value: "1")
            DataStatistics.sendEvent(event: event, msg: ext)
        }
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    // 选择边框View
    private lazy var backImageView: UIImageView = UIImageView()
    
    // 自动续订
    private lazy var autoRenewalView: UIButton = {
        let view = UIButton()
        view.isUserInteractionEnabled = false
        view.setTitle("Auto-Renewal", for: .normal)
        view.setTitleColor(.white, for: .normal)
        let font = UIFont.boldSystemFont(ofSize: 8)
        view.titleLabel?.font = font
        view.setBackgroundImage("commodity_mb_autoR_icon".to_image, for: .normal)
        view.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -8)
        return view
    }()
    
    // 时间 + 背景图
    private lazy var dateView: UIButton = {
        let view = UIButton()
        view.isUserInteractionEnabled = false
        view.titleLabel?.font = UIFont.systemFont(ofSize: 16.f_scale)
        view.setTitleColor("#99773C".to_color, for: .normal)
        view.setTitleColor("#775E33".to_color, for: .selected)
        view.setBackgroundImage("commodity_mb_date_bgimg_normal".to_image, for: .normal)
        view.setBackgroundImage("commodity_mb_date_bgimg_sel".to_image, for: .selected)
        view.st_shadow(color: .black, opacity: 0.1, shadowOffset: CGSize(width: 4, height: 2))
        return view
    }()
    
    // 礼物图标
    private lazy var giftImageView: UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named:"commodity_gift")
        return view
    }()
    
    // 优惠券图标
    private lazy var couponImageView: UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named:"commodity_coupon_icon")
        return view
    }()
    
    // 现价格
    private lazy var priceLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#554422".to_color
        return view
    }()
    
    // 原价
    private lazy var invalidPriceLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#AAAAAA".to_color
        view.font = UIFont.regular(with: 14.f_scale)
        view.textAlignment = .center
        return view
    }()
    
    // 折扣比例
    private lazy var discountLabel: UILabel = {
        let view = UILabel()
        view.textAlignment = .center
        view.st_cornerRadius = discountLabelHeight * 0.5
        view.textColor = "#99773C".to_color
        view.font = UIFont.boldSystemFont(ofSize: 10)
        view.backgroundColor = "#FFEEC0".to_color
        return view
    }()
}

// MARK: - 初始化
extension MBCommodityDetailSpecCell {
    
    private func setupMakeAddSubviews() {
        
        contentView.addSubview(dateView)
        contentView.addSubview(backImageView)
        contentView.addSubview(autoRenewalView)
        contentView.addSubview(giftImageView)
        contentView.addSubview(priceLabel)
        contentView.addSubview(invalidPriceLabel)
        contentView.addSubview(discountLabel)
        contentView.addSubview(couponImageView)
    }
    
    private func setupMakeLayoutSubviews() {
        backImageView.snp.makeConstraints { make in
            make.top.equalTo(4)
            make.left.right.bottom.equalTo(contentView)
        }
        
        autoRenewalView.snp.makeConstraints { make in
            make.right.top.equalTo(contentView)
            make.width.equalTo(70)
            make.height.equalTo(14)
        }
        
        dateView.snp.makeConstraints { make in
            make.top.equalTo(4)
            make.left.right.equalTo(contentView)
            make.height.equalTo(38)
        }
        
        giftImageView.snp.makeConstraints { make in
            make.left.equalTo(2)
            make.top.equalTo(6)
            make.width.equalTo(14)
            make.height.equalTo(14)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.top.equalTo(dateView.snp.bottom).offset(4)
            make.left.right.equalTo(contentView)
            make.height.equalTo(24)
        }
        
        invalidPriceLabel.snp.makeConstraints { make in
            make.top.equalTo(priceLabel.snp.bottom).offset(4)
            make.left.right.equalTo(contentView)
            make.height.equalTo(14)
        }
        
        discountLabel.snp.makeConstraints { make in
            make.bottom.equalTo(contentView.snp.bottom).offset(-12)
            make.left.equalTo(8)
            make.right.equalTo(contentView.snp.right).offset(-8)
            make.height.equalTo(discountLabelHeight)
        }
        
        couponImageView.snp.makeConstraints { make in
            make.left.top.equalTo(backImageView)
            make.height.width.equalTo(28)
        }
    }
}
